//
//  RW_AddSongToSingleView.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/6.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_AddSongToSingleView.h"
#import "UIView+BlockGesture.h"
#import "RW_UserSingleModel.h"
#import "RW_LoginViewController.h"
#import "RW_UserSingleViewModel.h"
#import "RW_UserSingleItemViewCell.h"
#import "RW_UserSingleAddView.h"

@interface RW_AddSongToSingleView (){
    //歌曲信息
    RW_SongModel *_song_model;
    //歌单信息
    RW_UserSingleModel *_user_single_model;
}
/** 收藏功能相关VM */
@property (nonatomic,strong) RW_UserSingleViewModel *wlclass_view_model;
/** 关闭按钮 */
@property (nonatomic,strong) UIButton *wlclass_close_button;
/** 确定按钮 */
@property (nonatomic,strong) UIButton *wlclass_sure_button;
/** 展示列表 */
@property (nonatomic,strong) WLCollectionView *wlclass_collection;
@end

@implementation RW_AddSongToSingleView
#pragma mark -- 视图初始化 必须实现
/** 初始化视图数据 */
-(void)initView{
    self.backgroundColor = UIColor.whiteColor;
    
    self.layer.shadowOpacity = 0.2f;
    self.layer.shadowRadius = 5.f;
    self.layer.shadowOffset = CGSizeMake(0,0);
    self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.3f].CGColor;
    
    [self.contentView addSubview:self.wlclass_close_button];
    [self.contentView addSubview:self.wlclass_sure_button];
    [self.contentView addSubview:self.wlclass_collection];
    
    [self.wlclass_close_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    [self.wlclass_sure_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    [self.wlclass_collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView).mas_offset(5);
        make.top.mas_equalTo(self.wlclass_sure_button.mas_bottom);
    }];
}
/** 初始化视图数据 */
-(void)initData:(NSArray *)dataList{
    for (id object in dataList) {
        if ([object isKindOfClass:RW_SongModel.class]){
            _song_model = object;
        }
    }
    [self initViewData];
}
/** 根据数据加载视图 */
-(void)initViewData{
    [self.wlclass_view_model getAddSingleViewSuccess:^(NSArray<WLBaseCollectionViewLayerSection *> * _Nonnull response) {
        [self.wlclass_collection setAllSection:response];
    } fail:^(NSError * _Nonnull error) {
        [SVProgressHUD showError_wl:error.domain];
    }];
}
#pragma mark -- Base代理
/**
 可选
 视图弹出方向
 */
-(Base_General_Show_type)showViewLocation{
    return Base_General_Show_type_BottonSafe;
}
/**
 可选
 判断是否展示弹窗
 需要展示 则调用isShowBlock()回调
 默认展示
 */
-(void)isShowModelList:(NSArray<id> *)dataList isShowBlock:(void(^)(void))isShowBlock{
    if (![UserModel_WL isLogin]){
        [RW_LoginViewController pushLoginFromVc:self.rootViewController];
        return;
    }
    NSAssert(isShowBlock, @"回调Block为空");
    isShowBlock();
}
/**
 可选
 是否需要处理点击确定后的操作逻辑
 点击确认按钮的 选中数据操作。
 需要子试图调用 defaultSureCloseViewData: 方法
 会回调 closeBlock_wl 的block块，内容为操作完毕后的内容
 默认回调 closeBlock_wl，无参数
 */
-(void)clickSelectCloseViewData:(id)data sureBlock:(void(^)(id data))sureBlock{
    NSDictionary *params = data;
    [SVProgressHUD showMask_wl];
    [self.wlclass_view_model addSongToSingleParams:params success:^(id  _Nonnull response) {
        [SVProgressHUD showSuccess_wl:@"操作成功"];
        sureBlock(response);
    } fail:^(NSError * _Nonnull error) {
        [SVProgressHUD showError_wl:error.domain];
    }];
}

#pragma mark -- 操作
-(void)clickSureButton:(UIButton *)sender{
    if (!_song_model){
        [SVProgressHUD showError_wl:@"意外的错误"];
        return;
    }
    if (!_user_single_model){
        [SVProgressHUD showError_wl:@"未选择歌单"];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:_song_model.xid forKey:@"songId"];
    [params setValue:_user_single_model.xid forKey:@"singleId"];
    [self defaultSureCloseViewData:params];
}

#pragma mark -- WLCollectionView 代理
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)layerModel
             layerRowData:(id)rowData
             didIndexPath:(NSIndexPath *)indexPath{
    for (WLBaseCollectionViewLayerSection *section in layerModel.viewLayer) {
        for (WLBaseCollectionViewLayerRow *row in section.item) {
            if ([row.data isKindOfClass:RW_UserSingleItemViewCellModel.class]){
                [(RW_UserSingleItemViewCellModel *)row.data setIsSelect:false];
            }
        }
    }
    if ([rowData isKindOfClass:RW_UserSingleItemViewCellModel.class]){
        _user_single_model = [(RW_UserSingleItemViewCellModel *)rowData user_single_model];
        [(RW_UserSingleItemViewCellModel *)rowData setIsSelect:true];
    }
    [self.wlclass_collection reloadData];
}

// 自定义显示视图
- (UIView *)wl_customViewForWLEmptyDataSet{
    if ([self.wlclass_collection initReload]){
        if (![UserModel_WL isLogin]){
            UIButton *button = [[UIButton alloc]init];
            [button setTitle:@"去登陆" forState:UIControlStateNormal];
            [button setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(click_go_login:) forControlEvents:UIControlEventTouchUpInside];
            return button;
        } else {
            UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            [activityIndicator startAnimating];
            return activityIndicator;
        }
    }
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:@"新建歌单" forState:UIControlStateNormal];
    [button setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(click_add_single) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

-(void)click_go_login:(UIButton *)sender{
    [RW_LoginViewController pushLoginFromVc:[self viewController_wl]];
}
-(void)click_add_single{
    __WEAK_SELF_YLSLIDE
    RW_UserSingleAddView.init_wl()
    .maskAlpha_wl(.5)
//    .rootViewController_wl([self viewController_wl])
    .closeBlock_wl(^(id data){
        if (data){
            [weakSelf initViewData];
        }
    }).show_wl();
}

#pragma mark -- 懒加载
-(RW_UserSingleViewModel *)wlclass_view_model{
    if (!_wlclass_view_model){
        _wlclass_view_model = [[RW_UserSingleViewModel alloc]init];
    }
    return _wlclass_view_model;
}
-(WLCollectionView *)wlclass_collection{
    if (!_wlclass_collection){
        _wlclass_collection = [WLCollectionView initWithVerticalDelegate:self];
        _wlclass_collection.backgroundColor = HexColor(@"f6f6f6");
    }
    return _wlclass_collection;
}
-(UIButton *)wlclass_close_button{
    if (!_wlclass_close_button){
        _wlclass_close_button = [[UIButton alloc]init];
        [_wlclass_close_button setTitle:@"取消" forState:UIControlStateNormal];
        _wlclass_close_button.titleLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
        [_wlclass_close_button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [_wlclass_close_button addTarget:self action:@selector(defaultCloseView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _wlclass_close_button;
}
-(UIButton *)wlclass_sure_button{
    if (!_wlclass_sure_button){
        _wlclass_sure_button = [[UIButton alloc]init];
        [_wlclass_sure_button setTitle:@"确定" forState:UIControlStateNormal];
        _wlclass_sure_button.titleLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
        [_wlclass_sure_button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [_wlclass_sure_button addTarget:self action:@selector(clickSureButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _wlclass_sure_button;
}
@end
